package cn.tedu.ivos.base.exception;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

/**
 * @author 482944566@qq.com
 * @version 1.0
 * @date 2024-11-29
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleServiceException(ServiceException e) {
        log.error("ServiceException:{}", e.getStatusCode().getMessage());
        return new JsonResult( e.getStatusCode());
    }
    @ExceptionHandler
    public  JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException:{}", e.getMessage());
        String message = e.getFieldError().getDefaultMessage();
        return new JsonResult(StatusCode.USERNAME_ERROR,message);
    }
    @ExceptionHandler
    public  JsonResult doHandleDuplicateKeyException(DuplicateKeyException e) {
        log.error("DuplicateKeyException:{}", e.getMessage());
        return new JsonResult(StatusCode.VALIDATE_ERROR);
    }

//    @ExceptionHandler
//    public JsonResult doHandleRuntimeException(RuntimeException e) {
//        log.error("RuntimeException:{}", e.getMessage());
//        return new JsonResult(StatusCode.OPERATION_FAILED);
//    }
}
